package wt.webPlugin;
import wt.entity.dictionary.MajorType;
import wt.entity.upms.Permission;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.List;


public class ZTree {
    public static final String ID = "id";
    public static final String PID = "pId";
    public static final String NAME = "name";
    public static final String CHECKED = "checked";
    public static final String DATA = "data";
    public static final String OPEN = "open";
    public static final String ICON = "open";
    public static final String ICON_SKIN = "iconSkin";



    public static JSONObject createZTreeJSONObject(String id, String pId, String name) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(ZTree.ID, id);
        jsonObject.put(ZTree.PID, pId);
        jsonObject.put(ZTree.NAME, name);
        return jsonObject;
    }

    public static JSONObject createZTreeJSONObject(String id, String pId, String name, Boolean checked) {
        JSONObject jsonObject = createZTreeJSONObject(id, pId, name);
        jsonObject.put(ZTree.CHECKED, checked);
        return jsonObject;
    }

    public static JSONObject createZTreeJSONObject(String id, String pId, String name, Object data) {
        JSONObject jsonObject = createZTreeJSONObject(id, pId, name);
        jsonObject.put(ZTree.DATA, data);
        return jsonObject;
    }

    public static JSONObject createZTreeJSONObject(String id, String pId, String name, Boolean checked, Object data) {
        JSONObject jsonObject = createZTreeJSONObject(id, pId, name);
        jsonObject.put(ZTree.CHECKED, checked);
        jsonObject.put(ZTree.DATA, data);
        return jsonObject;
    }


    public static void checkNodes(JSONArray zTreeJSONArray, List<String> checkIdList) {
        for (int i = 0; i < zTreeJSONArray.size(); i++) {
            JSONObject jsonObject = (JSONObject) zTreeJSONArray.get(i);
            if (checkIdList.contains(jsonObject.get(ZTree.ID))) {
                jsonObject.put(ZTree.CHECKED, true);
            }
        }
    }


    public static JSONArray permission2ZTreeJSONArray(List<Permission> permissionList) {
        String left="（",right="）";
        JSONArray jsonArray = new JSONArray();
        if (permissionList != null) {
            for (Permission permission : permissionList) {
                if (permission.getRemark()!=null){
                    jsonArray.add(createZTreeJSONObject(permission.getId(), permission.getPid(), permission.getName()+left+permission.getRemark()+right));
                }else{
                    jsonArray.add(createZTreeJSONObject(permission.getId(), permission.getPid(), permission.getName()));

                }

            }
        }
        return jsonArray;
    }


    /**
     * MajorType转成JSONArray
     * @param majorTypeList
     * @param checkedByLive  是否开启 根据 live字段 勾选
     * @return
     */
    public static JSONArray majorType2ZTreeJSONArray(List<MajorType> majorTypeList,boolean checkedByLive) {
        JSONArray jsonArray = new JSONArray();
        if (majorTypeList != null) {
            for (MajorType majorType : majorTypeList) {
                if (checkedByLive&&majorType.getLive() == 1 ) {
                    jsonArray.add(createZTreeJSONObject(majorType.getId(), majorType.getPid(), majorType.getName(), true));
                } else {
                    jsonArray.add(createZTreeJSONObject(majorType.getId(), majorType.getPid(), majorType.getName()));
                }
            }
        }
        return jsonArray;
    }
    
}
